#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: pymysql3-2

import pymysql
import json


def extDictFromJson(jsonStr):
    dic = json.loads(jsonStr)
    return dic


db = pymysql.connect(host="localhost",
                     user="root",
                     password="password",
                     port=13306,  # 端口
                     database="resultdb",
                     charset='utf8')

cursor = db.cursor()

sql = "SELECT result FROM bxHomePageBelowConten WHERE taskid = '{}'".format('0051055424271aaf255e1de14115bed8')
try:
    # 执行SQL语句
    cursor.execute(sql)
    # 获取所有记录列表
    results = cursor.fetchall()
    for row in results:
        result = row[0]
        resultStr = bytes(result).decode('utf-8')
        resDic = extDictFromJson(resultStr)
        # 打印结果
        print("result={}".format(resultStr))
        print()
        print("---------------- 图片信息 --------------")
        print("img_url={}".format(resDic['img_url']))
        print("file_name={}".format(resDic['file_name']))
        print("file_path={}".format(resDic['file_path']))
        print()
        print("---------------- 商品信息 --------------")
        print("goodsInfo.name={}".format(resDic['goodsInfo']['name']))
        print("goodsInfo.image={}".format(resDic['goodsInfo']['image']))
        print("goodsInfo.mallPrice={}".format(resDic['goodsInfo']['mallPrice']))
        print("goodsInfo.goodsId={}".format(resDic['goodsInfo']['goodsId']))
        print("goodsInfo.price={}".format(resDic['goodsInfo']['price']))
        print()
        print("---------------- 商品详情广告信息 --------------")
        print("advPicAddr={}".format(resDic['getGoodDetailById']['data']['advertesPicture']['PICTURE_ADDRESS']))
        print("advPicPlace={}".format(resDic['getGoodDetailById']['data']['advertesPicture']['TO_PLACE']))
        print("advPicUrlType={}".format(resDic['getGoodDetailById']['data']['advertesPicture']['urlType']))
        print()
        print("---------------- 商品详情信息 --------------")
        print("goodsId={}".format(resDic['getGoodDetailById']['data']['goodInfo']['goodsId']))
        print("goodsName={}".format(resDic['getGoodDetailById']['data']['goodInfo']['goodsName']))
        print("shopId={}".format(resDic['getGoodDetailById']['data']['goodInfo']['shopId']))
        print("isOnline={}".format(resDic['getGoodDetailById']['data']['goodInfo']['isOnline']))
        print("state={}".format(resDic['getGoodDetailById']['data']['goodInfo']['state']))
        print("amount={}".format(resDic['getGoodDetailById']['data']['goodInfo']['amount']))
        print("TYPE_SHIP={}".format(resDic['getGoodDetailById']['data']['goodInfo']['TYPE_SHIP']))
        print("goodsSerialNumber={}".format(resDic['getGoodDetailById']['data']['goodInfo']['goodsSerialNumber']))
        print("oriPrice={}".format(resDic['getGoodDetailById']['data']['goodInfo']['oriPrice']))
        print("presentPrice={}".format(resDic['getGoodDetailById']['data']['goodInfo']['presentPrice']))
        # 商品图片
        print("comPic={}".format(resDic['getGoodDetailById']['data']['goodInfo']['comPic']))
        print("image1={}".format(resDic['getGoodDetailById']['data']['goodInfo']['image1']))
        print("image2={}".format(resDic['getGoodDetailById']['data']['goodInfo']['image2']))
        print("image3={}".format(resDic['getGoodDetailById']['data']['goodInfo']['image3']))
        print("image4={}".format(resDic['getGoodDetailById']['data']['goodInfo']['image4']))
        print("image5={}".format(resDic['getGoodDetailById']['data']['goodInfo']['image5']))
        # 商品详情HTML
        print("goodsDetail={}".format(resDic['getGoodDetailById']['data']['goodInfo']['goodsDetail']))


except Exception as e:
    print("Error: unable to fecth data:" + str(e))


